<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Controller\AbstractController;
use Hyperf\Di\Annotation\Inject;
use App\Utils\ApiUtils;
use Hyperf\Utils\Context;
use App\Service\RbcService;
use App\Constants\ErrorCode;
use App\Validate\RbcValidation;

class RbacController extends AbstractController
{
    /**
     * @Inject 
     * @var RbcService
     */
    protected $service;


    //查找一个用户的所有权限(侧边栏)
    public function menu()
    {
        $jwt = Context::get('jwt_token');
        $user_id = $jwt['uid'];
        $result = $this->service->menu($user_id);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$result));
    }

   
    public function permissionList()
    {
        $jwt = Context::get('jwt_token');
        $user_id = $jwt['uid'];
        $result = $this->service->menu($user_id);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$result));

    }

    public function permissionAdd()
    {
        $data['identity'] = $this->request->input('identity');
        $data['name'] = $this->request->input('name');
        $data['sort_order'] = $this->request->input('sort_order', 0);
        $data['parent_id'] = $this->request->input('parent_id', 0);
        $data['url'] = $this->request->input('url', '');
        make(RbcValidation::class)->check($data,'add')->response();
        $result = $this->service->permissionAdd($data);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'添加成功'));
    }

    
    public function permissionEdit()
    {
        $id = $this->request->input('id');
        if($this->request->isMethod('post')){
            //接收参数
            $data['id'] = $id;
            $data['identity'] = $this->request->input('identity');
            $data['name'] = $this->request->input('name');
            $data['sort_order'] = $this->request->input('sort_order');
            $data['parent_id'] = $this->request->input('parent_id');
            $data['url'] = $this->request->input('url', '');
            make(RbcValidation::class)->check($data,'edit_post')->response();
            $result = $this->service->editPost($data);
            return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'修改成功'));

        }
        make(RbcValidation::class)->check(['id'=>$id],'edit_get')->response();
        $result = $this->service->editGet($id);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$result));
        
    }

    public function permissionDel()
    {
        $id = $this->request->input('id');
        make(RbcValidation::class)->check(['id'=>$id],'edit_get')->response();
        $result = $this->service->permissionDel($id);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'删除成功'));
        
    }

   
    public function roleList()
    {
        $page = $this->request->input('page', 1);
        $pageSize = $this->request->input('pageSize', 15);
        $result = $this->service->roleList($page,$pageSize);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$result['data'],$result['count']));
    }

   
    public function roleAll()
    {
        $jwt = Context::get('jwt_token');
        $user_id = $jwt['uid'];
        $result = $this->service->roleAll($user_id);
        return $this->response->json(ApiUtils::send(0,'获取成功',$result));

    }

    
    
    public function roleAdd()
    {
        //接收参数
        $data['name'] = $this->request->input('name');
        make(RbcValidation::class)->check($data,'role_add')->response();
        $jwt = Context::get('jwt_token');
        $data['admin_id'] = $jwt['uid'];
        $data['admin_name'] = $jwt['account'];
        $result = $this->service->roleAdd($data);
        return $this->response->json(ApiUtils::send(0,'添加成功',$result));
       
    }

   
    public function roleEdit()
    {
        $id = $this->request->input('id');
        if($this->request->isMethod('post')){
            //接收参数
            $data['id'] = $id;
            $data['name'] = $this->request->input('name');
            $jwt = Context::get('jwt_token');
            $data['admin_id'] = $jwt['uid'];
            $data['admin_name'] = $jwt['account'];
            make(RbcValidation::class)->check($data,'role_post')->response();
            $result = $this->service->rolePost($data);
            return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'修改成功'));

        }
        make(RbcValidation::class)->check(['id'=>$id],'edit_get')->response();
        $result = $this->service->roleGet($id);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$result));
    }

    
    public function rolePermisionEdit()
    {
        $role_id = $this->request->input('role_id');
        if($this->request->isMethod('post')){
            //接收参数
            $data['role_id'] = $role_id;
            $data['permission_id'] = $this->request->input('permission_id');
            make(RbcValidation::class)->check($data,'role_per_post')->response();
            $result = $this->service->rolePermisionPost($data);
            return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'修改成功'));

        }
        make(RbcValidation::class)->check(['role_id'=>$role_id],'role_per_get')->response();
        $result = $this->service->rolePermisionGet($role_id);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$result));

    }

    
    public function roleDel()
    {
        $id = $this->request->input('id');
        make(RbcValidation::class)->check(['id'=>$id],'edit_get')->response();
        $result = $this->service->roleDel($id);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'删除成功'));
        
    }


   

}